﻿using Delonix_Regia_HMS.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delonix_Regia_HMS
{
    public partial class CustomerCheckIn : Form
    {
        public CustomerCheckIn()
        {
            InitializeComponent();
            lblCustomerCheckInTodayDate.Text = DateTime.Now.ToString();
        }

        private void onlyNumbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void OnlyCharacters_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
        }

        private void btnCustomerCheckInSearchByBookingId_Click(object sender, EventArgs e)
        {
            tbxCustomerCheckInSearchNRIC.Clear();
            tbxCustomerCheckInSearchCustomerName.Clear();
            if (string.IsNullOrEmpty(tbxCustomerCheckInSearchBookingId.Text))
            {
                MessageBox.Show("Please enter a Booking Id to Search!");
            }
            else
            {
                DBManager.DisplayBookingsByBookingId(listViewCustomerCheckInBookingInformation, Convert.ToInt32(tbxCustomerCheckInSearchBookingId.Text));
            }
        }

        private void btnCustomerCheckInSearchByCustomerNRIC_Click(object sender, EventArgs e)
        {
            tbxCustomerCheckInSearchBookingId.Clear();
            tbxCustomerCheckInSearchCustomerName.Clear();
            if (string.IsNullOrEmpty(tbxCustomerCheckInSearchNRIC.Text))
            {
                MessageBox.Show("Please enter the Customer NRIC to Search!");
            }
            else
            {
                DBManager.DisplayBookingsByCustomerNRIC(listViewCustomerCheckInBookingInformation, tbxCustomerCheckInSearchNRIC.Text);
            }  
        }

        private void btnCustomerCheckInDisplayAllBookings_Click(object sender, EventArgs e)
        {
            DBManager.DisplayAllBookings(listViewCustomerCheckInBookingInformation);
        }

        private void btnCustomerCheckInSearchByCustomerName_Click(object sender, EventArgs e)
        {
            tbxCustomerCheckInSearchBookingId.Clear();
            tbxCustomerCheckInSearchNRIC.Clear();
            if (string.IsNullOrEmpty(tbxCustomerCheckInSearchCustomerName.Text))
            {
                MessageBox.Show("Please enter the Customer First Name to Search!");
            }
            else
            {
                DBManager.DisplayBookingsByCustomerFirstName(listViewCustomerCheckInBookingInformation, tbxCustomerCheckInSearchCustomerName.Text);
            }  
            
        }

        private void btnCheckInCustomer_Click(object sender, EventArgs e)
        {
            bool successfulCreate = false;
            if (string.IsNullOrEmpty(tbxCustomerCheckInBookingId.Text))
            {
                MessageBox.Show("Please Select a Booking to Check In!");
            }
            else if (string.IsNullOrEmpty(cbxCustomerCheckInRoomId.Text))
            {
                MessageBox.Show("Please Select a Room to check the customer in!");
            }
            else if (tbxCustomerCheckInActualCheckInTime.TextLength < 4)
            {
                MessageBox.Show("Please Enter the Actual Check In Time in 24 hour format from 0000  to 2359. E.g. If 1am enter 0100");
            }
            else if (Convert.ToInt32(tbxCustomerCheckInActualCheckInTime.Text) > 2359 || Convert.ToInt32(tbxCustomerCheckInActualCheckInTime.Text) < 0000)
            {
                MessageBox.Show("Please Enter The Actual Check In Time in 24 hour format from 0000(12am)  to 2359(11.59pm).");
            }
            else if (string.IsNullOrEmpty(tbxCustomerCheckInActualCheckInTime.Text))
            {
                MessageBox.Show("Please Enter the Actual Check In Time of the customer!");
            }
            else//Everything filled up
            {
                CheckIn cI = new CheckIn();
                cI.BookingId = Convert.ToInt32(tbxCustomerCheckInBookingId.Text);
                cI.RoomId = Convert.ToInt32(cbxCustomerCheckInRoomId.Text);
                cI.ActualCheckInDate = DateTime.Now;
                cI.ActualCheckInTime = tbxCustomerCheckInActualCheckInTime.Text;
                DBManager.CustomerCheckIn(cI);
                DBManager.UpdateRoomStatusCheckIn(Convert.ToInt32(cbxCustomerCheckInRoomId.Text));
                DBManager.UpdateBookingStatusCheckIn(Convert.ToInt32(cbxCustomerCheckInRoomId.Text));
                successfulCreate = true;
                DBManager.UpdateBookingStatusCheckIn(Convert.ToInt32(tbxCustomerCheckInBookingId.Text));
            }
            if (successfulCreate == true)
            {
                MessageBox.Show("Create Successful");
            }
            else
            {
                MessageBox.Show("Create NOT Successful");
            }
            this.Close();
        }

        private void listViewCustomerCheckInBookingInformation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCustomerCheckInBookingInformation.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listViewCustomerCheckInBookingInformation.SelectedItems)
                {
                    tbxCustomerCheckInBookingId.Text = listViewCustomerCheckInBookingInformation.SelectedItems[0].Text;
                    DBManager.FillComboxBoxRoomID(cbxCustomerCheckInRoomId ,Convert.ToInt32(tbxCustomerCheckInBookingId.Text));
                }
            }
        }                   
    }
}
